package com.systemic.service.impl;

import com.systemic.mapper.DynamicMapper;
import com.systemic.mapper.LikeMapper;
import com.systemic.pojo.Dynamic;
import com.systemic.pojo.Like;
import com.systemic.service.LikeService;
import com.systemic.vo.LikeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @ClassName: LikeServiceImpl
 * @Description:
 * @Author:
 * @CreateTime: 2024-01-21 10:04
 * @Version:
 **/
@Service
public class LikeServiceImpl implements LikeService {

    @Resource
    private LikeMapper likeMapper;
    @Resource
    private DynamicMapper dynamicMapper;
    /**
     * 点赞
     * @param likeVo
     * @return
     */
    @Override
    @Transactional
    public int create(LikeVo likeVo) {
        int row = 0;
        if (likeVo!=null){
            Like like = new Like();
            Integer did = likeVo.getDid();
            like.setDid(did);
            like.setUid(likeVo.getUid());
            // 查询用户的点赞记录
            Like liked = likeMapper.selectByDidAndUid(like);
            if (liked==null){
                Dynamic dynamic = new Dynamic();
                dynamic.setId(did);
                // 先查询当前动态的点赞量，如果为空直接insert，否则做更新操作
                Dynamic res = dynamicMapper.selectById(did);
                if (res!=null){
                    // 执行更新动态的点赞量
                    if (res.getLikeNum()==null){
                        dynamic.setLikeNum(1);
                    } else if(res.getLikeNum()==0){
                        dynamic.setLikeNum(1);
                    } else{
                        dynamic.setLikeNum(res.getLikeNum()+1);
                    }
                }
                dynamicMapper.update(dynamic);
                row = likeMapper.insert(like);
            } else {
                throw new RuntimeException("当前用户已经点赞过");
            }
        }
        return row;
    }

    /**
     * 取消点赞
     * @param likeVo
     * @return
     */
    @Override
    @Transactional
    public int delete(LikeVo likeVo) {
        int row = 0;
        if (likeVo!=null){
            Like like = new Like();
            Integer did = likeVo.getDid();
            like.setDid(did);
            like.setUid(likeVo.getUid());

            // 查询用户的点赞记录
            Like liked = likeMapper.selectByDidAndUid(like);
            if (liked==null){
                Dynamic dynamic = new Dynamic();
                dynamic.setId(did);
                // 先查询当前动态的点赞量，如果为空直接insert，否则做更新操作
                Dynamic res = dynamicMapper.selectById(did);
                if (res!=null){
                    // 执行更新动态的点赞量
                    if (res.getLikeNum()!=0){
                        dynamic.setLikeNum(res.getLikeNum()-1);
                    } else if(res.getLikeNum()!=null){
                        dynamic.setLikeNum(res.getLikeNum()-1);
                    }
                }
                dynamicMapper.update(dynamic);
                row = likeMapper.delete(like);
            }else {
                throw new RuntimeException("当前用户没有点赞过，无需删除");
            }
        }
        return row;
    }
}
